const { Article } = require('../../model/article');
const { Comment } = require('../../model/comment');
const { Category } = require('../../model/Category');
module.exports = async (req,res)=>{
  let category = await Category.find();
  //查询热门文章，前五个数据
  let hotArticle = await Article.find().sort({readCount:-1}).limit(5);
  let article = await Article.findOne({_id: req.query.id}).populate('author').populate('classify');
  //根据id查询文章对应的评论
  let comment = await Comment.find({aid: req.query.id,state: 1}).populate('uid');
  article = JSON.stringify(article);
  article = JSON.parse(article);
  comment = JSON.stringify(comment);
  comment = JSON.parse(comment);
  if(article){
    //每点一次，设置阅读量+1
    await Article.findByIdAndUpdate(req.query.id,{
      readCount: article.readCount + 1
    });
    res.render('home/article_details',{
      article,
      comment,
      category,
      hotArticle
    });
  }else {
    res.render('/404',{
      message: '资源未找到！！！'
    });
  }
}